<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Sentence;
use yii;


class SentenceLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getSentence($sentence_type,$query = [], $page = 1, $limit = 10)
    {
        $sentence = Sentence::find()->filterWhere($query)->andWhere("sentence_type=$sentence_type")->andWhere("is_del=0")
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $sentence->asArray()->all();
        if($sentence_type==0){
            $url = 'sentence/chiness';
        }else{
            $url = 'sentence/spanish';
        }

        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $sentence->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => $url,
                'query' => $query
            ]
        ];
        return $return;
    }




    public function getById($id)
    {
        return Sentence::find()->where(['sentence_id' => $id])->asArray()->one();
    }


    public function saveStatus($id)
    {
        $article = Article::findOne($id);
        if($article->status)
            $article->status = 0;
        else
            $article->status = 1;
        return $article->save();
    }

    public function addSentence($params){
        $sentence = new Sentence();
        $sentence->sentence_name = $params['sentence_name'];
        $sentence->sentence_content = $params['sentence_content'];
        $sentence->add_time = date('Y-m-d H:i:s');
        $sentence->sentence_type = $params['sentence_type'];
        $sentence->sentence_extend_type = $params['sentence_extend_type'];
        return $sentence->save();
    }

    public function editById($id, $params){
        if(!$sentence = Sentence::findOne($id)) return false;
        $sentence->sentence_name = $params['sentence_name'];
        $sentence->sentence_content = $params['sentence_content'];
        $sentence->sentence_extend_type = $params['sentence_extend_type'];
        return $sentence->save();
    }

    public function delete($id){
        if(!$sentence = Sentence::findOne($id)) return false;
        $sentence->is_del = 1;
        return $sentence->save();
        /*return $sentence->delete();*/
    }



}
